<script type="text/javascript"
	src="../js/jquery-easyui-1.3.5/jquery.min.js" charset="UTF-8"></script>
<script type="text/javascript"
	src="../js/jquery-easyui-1.3.5/jquery.easyui.min.js" charset="UTF-8"></script>
<link rel="stylesheet"
	href="../js/jquery-easyui-1.3.5/themes/default/easyui.css"
	type="text/css" />
<link rel="stylesheet" href="../js/jquery-easyui-1.3.5/themes/icon.css"
	type="text/css" />
<link rel="stylesheet" href="../css/main.css" type="text/css" />
<script type="text/javascript" src="../js/mine.js"></script>
<script type="text/javascript"
	src="../js/jquery-easyui-1.3.5/locale/easyui-lang-zh_CN.js"
	charset="UTF-8"></script>
 <script type="text/javascript" src="../js/validate.js" charset="UTF-8"></script> 
<script type="text/javascript" charset="UTF-8">
	var managedStudentDatagrid;
	var managedStudentLayout;
	var managedStudentForm;
	var sexData = [{
		"id":"male",
		"text":"男"
	},{
		"id":"female",
		"text":"女",
	}];
	
	var stypeData = [{
		"id":"designer",
		"text":"美工"
	},{
		"id":"frontend",
		"text":"前端",
	},{
		"id":"backend",
		"text":"后台"
	}];
	
	$(function(){
		managedStudentForm = $('#managedStudentForm').form();
		managedStudentLayout = $('#managedStudentLayout').layout();
		managedStudentDatagrid = $('#managedStudentDatagrid').datagrid({
			url:'../servlet/GetMStudentServlet',
			//title:'学生信息',
			//iconCls:'icon-save',
			fitColumns:true,
			nowrap:false,
			//striped:true,
			idField:'sno',
			pagination:true,
			pageSize:10,
			pageList:[10,20,30],
			fit:true,
			border:false,
			sortName:'sno',
			sortOrder:'desc',
			//showFooter:true,
			columns:[[{
				field:'cut',
				checkbox:true
			},{
				title:'学号',
				field:'sno',
				width:100,
				sortable:true
			},{
				title:'姓名',
				field:'sname',
				width:100,
				sortable:true
			},{
				title:'性别',
				field:'sex',
				width:100,
				sortable:true,
				formatter:function(value,rowData,rowIndex){
					 for(var i=0; i<sexData.length; i++){  
			                if (sexData[i].id == value) {
			                	return sexData[i].text;  
			                }
		            }  
				}
			},{
				title:'专业',
				field:'major',
				width:100
			},{
				title:'年级',
				field:'grade',
				width:100
			},{
				title:'分工',
				field:'stype',
				width:100
			},{
				title:'参与项目数',
				field:'proNum',
				width:100,
				formatter: function(value,rowData,rowIndex){
					if(value != 0) {
		            	return '<a href="#" onclick="showProject(' + rowIndex + ');">' + value + '</a>';
					} else {
						return value;
					}
		     	} 
			},{
				title:'参与我的项目数',
				field:'speProNum',
				width:10,
				hidden:true
			}]],
			toolbar:[{
				text:'添加',
				iconCls:'icon-add',
				handler:function(){
					 var menuTab = parent.$('#menuTab', parent.document).tabs();//得到tabs对象
						var tabText = '未管理学生';
						var tabUrl = 'teacher/notMStudentData.html';
						//console.info(menuTab);
			  			if(menuTab.tabs('exists', tabText)) {
			  				menuTab.tabs('select', tabText);
			  			} else {
		  					menuTab.tabs('add',{
		  						title: tabText,
		  						closable:true,
		  						content:'<iframe src="'+ tabUrl +'" frameborder="0" style="border:0;width:100%;height:100%;"></iframe>'
		  					});
			  			}
				}
			},'-',{
				text:'删除',
				iconCls:'icon-cancel',
				handler:function(){
					var rows = managedStudentDatagrid.datagrid('getSelections');
					var ids = [];
					var cantd = [];
					var m = '';
					var cantm = '';
					
					console.info(rows.length);
					if(rows.length > 0) {
						for(var i=0;i<rows.length; i++) {
							if(rows[i].speProNum == '0') {
								ids.push(rows[i].sno);
							} else {
								cantd.push(rows[i].sno);
								cantm += "<br/> [" + rows[i].sno + "&nbsp;&nbsp;&nbsp;&nbsp;" + rows[i].sname + "]";
							}
						}
						
						console.info(ids);
						console.info(cantd);
						
						if(cantd.length > 0 && ids.length == 0) {//选择的数据都不能删除
							m = '选择的数据都不能删除。';
							$.messager.alert('提示',m,'warning');
						} else {
							if(cantd.length > 0 && ids.length > 0){//选择的数据部分不能删除
								m = '<div style="width:10px;margin:auto auto auto 45px;">选择的数据中:' + cantm + '<br/>参与的项目未完成,不能删除.<br>确定要删除选中的其余数据吗?</div>';
							} else if(cantd.length == 0 && ids.length > 0) {//选择的数据都可以删除
								m = '确定要删除所选择的学生吗?';
							}
						
							$.messager.confirm('提示',m,function(b) {
								if(b) {
									$.ajax({
										url:'../servlet/DeleteMStudentServlet',
										data:{
											ids:ids.join(',')//用,分隔
										},
										type:'post',
										dataType:'json',
										success:function(r) {
											if(r && r.success) {
												managedStudentDatagrid.datagrid('acceptChanges');
												managedStudentDatagrid.datagrid('load');
												managedStudentDatagrid.datagrid('unselectAll');
												managedStudentDatagrid.datagrid('rejectChanges');
												//console.info(managedStudentDatagrid.datagrid('getSelections'));
												$.messager.show({
													title:'提示',
													msg:r.msg
												});
											} else {
												$.messager.alert('提示',r.msg,'error');
											}
										}
									});
								}
							});
						}
					} else {
						$.messager.alert('提示','未选择任何数据.','warning');
					} 
				}
			},'-',{
				text:'取消',
				iconCls:'icon-undo',
				handler:function(){
					managedStudentDatagrid.datagrid('unselectAll');
					managedStudentDatagrid.datagrid('rejectChanges');
				}
			},'-',{
				text:'刷新',
				iconCls:'icon-reload',
				handler:function(){
					managedStudentDatagrid.datagrid('unselectAll');
					managedStudentDatagrid.datagrid('rejectChanges');
					managedStudentDatagrid.datagrid('load');
				}
			},'-']
		});
		
		$('#stype').combobox({
			data:[{
				"id":"all",
				"text":"全部",
			},{
				"id":"美工",
				"text":"美工"
			},{
				"id":"前端",
				"text":"前端",
			},{
				"id":"后台",
				"text":"后台"
			}],
			valueField:'id',
			textField:'text',
			delay:1000
		});
		
		$('#sex').combobox({
			data:[{
				"id":"all",
				"text":"全部",
			},{
				"id":"male",
				"text":"男"
			},{
				"id":"female",
				"text":"女",
			}],
			valueField:'id',
			textField:'text',
			editable:false
	});
});
	
showProject = function(i) {
	var rows = managedStudentDatagrid.datagrid('getRows');
	var title = '参与项目_' + rows[i].sname;
	console.info(title);
	showProjectDialog = parent.$('#showProjectDialog', parent.document).dialog({
		title:title,
		modal:true,
		method:'post',
		collapsible:false,
		showHeader:false,
		width:500,
		height:500,
		href:'teacher/showProjectData.jsp?sno=' + rows[i].sno
	});
}
	
searchStudent = function() {
	console.info(serializeObject(managedStudentForm));
	managedStudentDatagrid.datagrid('load',serializeObject(managedStudentForm));
}

cleanStudent = function() {
	managedStudentDatagrid.datagrid('load',{});
	managedStudentForm.find('input').val('');
}
</script>

<div id="managedStudentLayout" fit="true" border="false">
	<div region="north" border="false" split="false" title="查询" style="margin:0 auto;height:81px;" >
		<form id="managedStudentForm" class="datagrid-toolbar"
			style="margin: 0 auto;">
				<table>
					<tr>
						<td><b>学号</b><input type="text" name="sno" class="easyui-validatebox"  placeholder="您只能输入数字" data-options="validType:'nums'"  /></td>
						<td><b>姓名</b><input type="text" name="sname" /></td>
						<td><b>性别</b><input type="text" name="sex" id="sex" /></td>
					</tr>
					<tr>
						<td><b>专业</b><input type="text" name="major" /></td>
						<td><b>年级</b><input type="text" name="grade" class="easyui-validatebox" placeholder="您只能输入4位数字" data-options="validType:'grade[4]'" /></td>
						<td><b>分工</b><input type="text" name="stype" id="stype" /></td>
						<td><a href="javascript:void(0);" class="easyui-linkbutton"
							iconCls="icon-search" pain="false" onclick="searchStudent();"></a>
						</td>
						<td><a href="javascript:void(0);" class="easyui-linkbutton"
							pain="false" onclick="cleanStudent();">清空</a>
						</td>

					</tr>
				</table>
		</form>
	    </div>
	 <div region="center" border="false">
		<table id="managedStudentDatagrid" fit="true"></table>
	</div>
</div>
